home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_08 / 1108074a < prev    next >
Text File  |  1993-06-06  |  27KB  |  867 lines

  1.      /*******************************************
  2.      *
  3.      *   special_opening(...
  4.      *
  5.      *   Opening is erosion followed by dilation.
  6.      *   This routine will use the thinning
  7.      *   erosion routine.  This will not allow
  8.      *   an object to erode to nothing.
  9.      *
  10.      *   The number parameter specifies how
  11.      *   erosions to perform before doing one
  12.      *   dilation.
  13.      *
  14.      *******************************************/
  15.  
  16. special_opening(in_name, out_name, the_image,
  17.                 out_image, il, ie, ll, le,
  18.                 value, threshold, number)
  19.    char   in_name[], out_name[];
  20.    int    il, ie, ll, le, number;
  21.    short  the_image[ROWS][COLS],
  22.           out_image[ROWS][COLS],
  23.           threshold, value;
  24. {
  25.    int    a, b, count, i, j, k;
  26.    int    length, width;
  27.    struct tiff_header_struct image_header;
  28.  
  29.    if(does_not_exist(out_name)){
  30.       printf("\n\n output file does not exist %s", out_name);
  31.       read_tiff_header(in_name, &image_header);
  32.       round_off_image_size(&image_header,
  33.                            &length, &width);
  34.       image_header.image_length = length*ROWS;
  35.       image_header.image_width  = width*COLS;
  36.       create_allocate_tiff_file(out_name, &image_header,
  37.                                 out_image);
  38.    }  /* ends if does_not_exist */
  39.  
  40.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  41.  
  42.    thinning(in_name, out_name, the_image,
  43.             out_image, il, ie, ll, le,
  44.             value, threshold, 1);
  45.  
  46.    if(number > 1){
  47.       count = 1;
  48.       while(count < number){
  49.          count++;
  50.          thinning(out_name, out_name, the_image,
  51.                   out_image, il, ie, ll, le,
  52.                   value, threshold, 1);
  53.       }  /* ends while */
  54.    }  /* ends if number > 1 */
  55.  
  56.    dilation(out_name, out_name, the_image,
  57.             out_image, il, ie, ll, le,
  58.             value, threshold);
  59.  
  60.    write_array_into_tiff_image(out_name, out_image,
  61.                                il, ie, ll, le);
  62.  
  63. }  /* ends special_opening */
  64.  
  65.  
  66.  
  67.  
  68.      /*******************************************
  69.      *
  70.      *   thinning(...
  71.      *
  72.      *   Use a variation of the grass fire
  73.      *   wave front approach.
  74.      *
  75.      *   Raster scan the image left to right
  76.      *   and examine and thin the left edge pixels
  77.      *   (a 0 to value transition).  Process them
  78.      *   normally and "save" the result.  Next,
  79.      *   raster scan the image right to left and
  80.      *   save.  Raster scan top to bottom and save.
  81.      *   Raster scan bottom to top and save.
  82.      *
  83.      *   That is one complete pass.
  84.      *
  85.      *   Keep track of pixels thinned for a
  86.      *   pass and quit when you make a complete
  87.      *   pass without thinning any pixels.
  88.      *
  89.      *******************************************/
  90.  
  91. thinning(in_name, out_name, the_image, out_image,
  92.          il, ie, ll, le, value, threshold, once_only)
  93.    char   in_name[], out_name[];
  94.    int    il, ie, ll, le, once_only;
  95.    short  the_image[ROWS][COLS],
  96.           out_image[ROWS][COLS],
  97.           threshold, value;
  98. {
  99.    int    a, b, big_count, count, i, j, k,
  100.           not_finished;
  101.    int    length, width;
  102.    struct tiff_header_struct image_header;
  103.  
  104.    if(does_not_exist(out_name)){
  105.       printf("\n\n output file does not exist %s", out_name);
  106.       read_tiff_header(in_name, &image_header);
  107.       round_off_image_size(&image_header,
  108.                            &length, &width);
  109.       image_header.image_length = length*ROWS;
  110.       image_header.image_width  = width*COLS;
  111.       create_allocate_tiff_file(out_name, &image_header,
  112.                                 out_image);
  113.    }  /* ends if does_not_exist */
  114.  
  115.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  116.  
  117.  
  118.    for(i=0; i<ROWS; i++)
  119.       for(j=0; j<COLS; j++)
  120.          out_image[i][j] = the_image[i][j];
  121.  
  122.    not_finished = 1;
  123.    while(not_finished){
  124.  
  125.       if(once_only == 1)
  126.         not_finished = 0;
  127.       big_count = 0;
  128.  
  129.          /***************************
  130.          *
  131.          *   Scan left to right
  132.          *   Look for 0-value transition
  133.          *
  134.          ****************************/
  135.  
  136.       printf("\n");
  137.       for(i=1; i<ROWS-1; i++){
  138.          if( (i%10) == 0) printf("%3d", i);
  139.          for(j=1; j<COLS-1; j++){
  140.             if(the_image[i][j-1] == 0   &&
  141.                the_image[i][j]   == value){
  142.                count = 0;
  143.                for(a=-1; a<=1; a++){
  144.                    for(b=-1; b<=1; b++){
  145.                          if(the_image[i+a][j+b] == 0)
  146.                             count++;
  147.                    }  /*  ends loop over b */
  148.                }  /* ends loop over a */
  149.                if(count > threshold){
  150.                   if(can_thin(the_image, i, j, value)){
  151.                      out_image[i][j] = 0;
  152.                      big_count++;
  153.                   }  /* ends if can_thin */
  154.                }  /* ends if count > threshold */
  155.             }  /* ends if the_image == value */
  156.          }  /* ends loop over j */
  157.       }  /* ends loop over i */
  158.  
  159.          /**************************************
  160.          *
  161.          *   Copy the output back to the input.
  162.          *
  163.          **************************************/
  164.  
  165.       for(i=0; i<ROWS; i++)
  166.          for(j=0; j<COLS; j++)
  167.             the_image[i][j] = out_image[i][j];
  168.  
  169.  
  170.          /***************************
  171.          *
  172.          *   Scan right to left
  173.          *   Do this by scanning left
  174.          *   to right and look for
  175.          *   value-0 transition.
  176.          *
  177.          ****************************/
  178.  
  179.       printf("\n");
  180.       for(i=1; i<ROWS-1; i++){
  181.          if( (i%10) == 0) printf("%3d", i);
  182.          for(j=1; j<COLS-1; j++){
  183.             if(the_image[i][j+1] == 0   &&
  184.                the_image[i][j]   == value){
  185.                count = 0;
  186.                for(a=-1; a<=1; a++){
  187.                    for(b=-1; b<=1; b++){
  188.                          if(the_image[i+a][j+b] == 0)
  189.                             count++;
  190.                    }  /*  ends loop over b */
  191.                }  /* ends loop over a */
  192.                if(count > threshold){
  193.                   if(can_thin(the_image, i, j, value)){
  194.                      out_image[i][j] = 0;
  195.                      big_count++;
  196.                   }  /* ends if can_thin */
  197.                }  /* ends if count > threshold */
  198.             }  /* ends if the_image == value */
  199.          }  /* ends loop over j */
  200.       }  /* ends loop over i */
  201.  
  202.          /**************************************
  203.          *
  204.          *   Copy the output back to the input.
  205.          *
  206.          **************************************/
  207.       for(i=0; i<ROWS; i++)
  208.          for(j=0; j<COLS; j++)
  209.             the_image[i][j] = out_image[i][j];
  210.  
  211.  
  212.          /***************************
  213.          *
  214.          *   Scan top to bottom
  215.          *   Look for 0-value transition
  216.          *
  217.          ****************************/
  218.  
  219.       printf("\n");
  220.       for(j=1; j<COLS-1; j++){
  221.          if( (j%10) == 0) printf("%3d", j);
  222.          for(i=1; i<ROWS-1; i++){
  223.             if(the_image[i-1][j] == 0   &&
  224.                the_image[i][j]   == value){
  225.                count = 0;
  226.                for(a=-1; a<=1; a++){
  227.                    for(b=-1; b<=1; b++){
  228.                          if(the_image[i+a][j+b] == 0)
  229.                             count++;
  230.                    }  /*  ends loop over b */
  231.                }  /* ends loop over a */
  232.                if(count > threshold){
  233.                   if(can_thin(the_image, i, j, value)){
  234.                      out_image[i][j] = 0;
  235.                      big_count++;
  236.                   }  /* ends if can_thin */
  237.                }  /* ends if count > threshold */
  238.             }  /* ends if the_image == value */
  239.          }  /* ends loop over i */
  240.       }  /* ends loop over j */
  241.  
  242.          /**************************************
  243.          *
  244.          *   Copy the output back to the input.
  245.          *
  246.          **************************************/
  247.  
  248.       for(i=0; i<ROWS; i++)
  249.          for(j=0; j<COLS; j++)
  250.             the_image[i][j] = out_image[i][j];
  251.  
  252.  
  253.